﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using School.Entities;
using System.Data;
using School.Teaching;
using System.Drawing;

namespace School.Pages.Teaching
{
    public partial class TE_Subject_Success_Policy : System.Web.UI.Page
    {
        private static SchoolEntities db = new SchoolEntities();
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((IQueryable<int>)Session["privilage"] != null)
            {
                var x = (IQueryable<int>)Session["privilage"];

                if (x.Contains(1512) == true)
                {
                    if (!this.Page.IsPostBack)
                    {
                        FillControls();
                    }
                    lbltest.Visible = false;
                    lblgrid.Visible = false;
                }
                else
                    Response.Redirect("../organization/no_privelege.aspx");
            }
            else
                Response.Redirect("../organization/no_privelege.aspx");

        }

        private void FillControls()
        {
            ddlCampus.DataSource = School.Uniform.PayOrder.AllCampus();
            ddlCampus.DataBind();
        }

        private void ddlBind(DropDownList ddl)
        {
            ddl.Items.Clear();
            ddl.Items.Insert(0, new ListItem("اختيار", "0"));
            ddl.DataBind();
        }
        protected void ddlCertificate_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlCertificate.SelectedValue != "0")
            {
                ddlStadge.DataSource = School.Uniform.PayOrder.GetStadgeByCertificate(int.Parse(ddlCertificate.SelectedValue));
                ddlBind(ddlStadge);
            }

        }
        protected void ddlStadge_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlStadge.SelectedValue != "0")
            {
                ddlGrade.DataSource = School.Uniform.PayOrder.GetGradeByStadge(int.Parse(ddlStadge.SelectedValue));
                ddlBind(ddlGrade);
            }

        }
        protected void ddlCampus_TextChanged(object sender, EventArgs e)
        {
            if (ddlCampus.SelectedValue != "0")
            {

                ddlCertificate.DataSource = School.Uniform.PayOrder.GetCertificateByCamp(int.Parse(ddlCampus.SelectedValue));
                ddlBind(ddlCertificate);
            }
        }

        private void ddlBind(DropDownList ddl, object o)
        {
            ddl.DataSource = o;
            ddl.Items.Clear();
            ddl.Items.Insert(0, new ListItem("اختيار", "0"));
            ddl.DataBind();
        }

        protected void ddlGrade_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlGrade.SelectedIndex > 0)
            {
                ddlBind(ddlsubject, School.Teaching.Teaching.Get_Subject_By_Grad(Convert.ToInt32(ddlGrade.SelectedValue)));
            }
        }

        protected void ddlsubject_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlsubject.SelectedIndex > 0)
            {
                gv_Exam_SubBind();
            }
            Session["gv"] = null;
            gvExam.DataSource = (DataTable)Session["gv"];
            gvExam.DataBind();
            fsExam.Visible = false;
        }

        private void gv_Exam_SubBind()
        {
            int id = Convert.ToInt32(ddlsubject.SelectedValue.ToString());
            var query = from v in db.V_Exam where v.Id == id select v;
            gv_Exam_Sub.DataSource = query;
            gv_Exam_Sub.DataBind();
            if (gv_Exam_Sub.Rows.Count > 0)
                exam_sub.Visible = true;
            else
                exam_sub.Visible = false;
        }
        private static void DataTable(out DataTable dt)
        {
            dt = new DataTable();
            dt.Columns.Add("Id", typeof(int));
            dt.Columns.Add("Sub_Id", typeof(int));
            dt.Columns.Add("Exam_Id", typeof(int));
            dt.Columns.Add("Sub", typeof(string));
            dt.Columns.Add("Exam", typeof(string));
            dt.Columns.Add("Per", typeof(decimal));
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            DataTable dt;
            DataRow drp;
            DataTable(out dt);
            bool re = true;
            foreach (GridViewRow Row in gvExam.Rows)
            {
                Label lblExam = (Label)Row.FindControl("Exam");
                if (!string.IsNullOrEmpty(lblExam.Text))
                {
                    if (lblExam.Text == ddlExam.SelectedItem.Text)
                        re = false;
                }
                else
                    re = false;
            }

            if (re)
            {
                if (Session["gv"] == null)
                {
                    drp = dt.NewRow();

                    drp["Sub_Id"] = Convert.ToInt32(ddlsubject.SelectedValue);
                    drp["Exam_Id"] = Convert.ToInt32(ddlExam.SelectedValue);
                    drp["Sub"] = ddlsubject.SelectedItem.Text;
                    drp["Exam"] = ddlExam.SelectedItem.Text;
                    drp["Per"] = txtpercent.Text;
                    dt.Rows.Add(drp);
                    Session["gv"] = dt;
                    gvExam.DataSource = dt;
                    gvExam.DataBind();

                }
                else
                {
                    dt = (DataTable)Session["gv"];
                    drp = dt.NewRow();

                    drp["Sub_Id"] = Convert.ToInt32(ddlsubject.SelectedValue);
                    drp["Exam_Id"] = Convert.ToInt32(ddlExam.SelectedValue);
                    drp["Sub"] = ddlsubject.SelectedItem.Text;
                    drp["Exam"] = ddlExam.SelectedItem.Text;
                    drp["Per"] = txtpercent.Text;
                    dt.Rows.Add(drp);
                    Session["gv"] = dt;
                    gvExam.DataSource = dt;
                    gvExam.DataBind();


                }

            }
            else
            {
                lbltest.Text = "الاختبار موجود من قبل";
                lbltest.Visible = true;
            }

            if (gvExam.Rows.Count > 0)
                fsExam.Visible = true;
            else
                fsExam.Visible = false;

        }
        protected void gvExam_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            DataTable td = (DataTable)Session["gv"];
            DataRow dr = td.Rows[e.RowIndex];
            dr.Delete();
            Session["gv"] = td;
            gvExam.DataSource = td;
            gvExam.DataBind();
        }

        protected void btnsave_Click(object sender, EventArgs e)
        {
            //insert in   TE_Subject_Success_policy
            long re = OfflineExam.Add_Subject_Success_polic(Convert.ToInt32(ddlsubject.SelectedValue), Convert.ToDecimal(txtMax.Text), Convert.ToDecimal(txtMin.Text));
            if (re > 0)
            {
                foreach (GridViewRow row in gvExam.Rows)
                {
                    int Exam_Id = Convert.ToInt32(gvExam.DataKeys[row.RowIndex].Values["Exam_Id"].ToString());
                    Label lblper = (Label)row.FindControl("lblper");
                    OfflineExam.Add_Subject_Policy_Exam(Exam_Id, Convert.ToInt32(re.ToString()), Convert.ToDecimal(lblper.Text));
                }
                lbltst.ForeColor = Color.Green;
                lbltst.Text = "تمت الاضافة بنجاح";
                txtMax.Text = "";
                txtMin.Text = "";
                txtpercent.Text = "";
                gv_Exam_SubBind();
            }
            else
            {
                lbltst.ForeColor = Color.Red;
                lbltst.Text = "لم تتم عملية الاضافة..حاول مرة اخري";
            }
            lbltst.Visible = true;
        }

        protected void gv_Exam_Sub_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //Delete_Subject_Success_polic
            int Exam_Id = Convert.ToInt32(gv_Exam_Sub.DataKeys[e.RowIndex].Values["Exam_Id"].ToString());
            int Id_Policy = Convert.ToInt32(gv_Exam_Sub.DataKeys[e.RowIndex].Values["Id_Policy"].ToString());
            bool re = OfflineExam.Delete_Subject_Policy_Exam(Exam_Id, Id_Policy);
            if (re == true)
            {
                lblgrid.ForeColor = Color.Green;
                lblgrid.Text = "تم الحذف بنجاح";
                gv_Exam_SubBind();
            }
            else
            {
                lblgrid.ForeColor = Color.Red;
                lblgrid.Text = "لم تتم عملية الحذف..حاول مرة اخري";
            }
            lblgrid.Visible = true;


        }

        protected void gv_Exam_Sub_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gv_Exam_Sub.EditIndex = -1;
            gv_Exam_SubBind();
        }

        protected void gv_Exam_Sub_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gv_Exam_Sub.EditIndex = e.NewEditIndex;
            gv_Exam_SubBind();
        }

        protected void gv_Exam_Sub_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int Id = Convert.ToInt32(gv_Exam_Sub.DataKeys[e.RowIndex].Values["Id_Policy"].ToString());
            TextBox txtmax = (TextBox)gv_Exam_Sub.Rows[e.RowIndex].FindControl("txtmax");
            TextBox txtmin = (TextBox)gv_Exam_Sub.Rows[e.RowIndex].FindControl("txtmin");

            bool re = OfflineExam.Updat_Subject_Success_polic(Id, Convert.ToDecimal(txtmax.Text), Convert.ToDecimal(txtmin.Text));
            if (re == true)
            {
                lblgrid.ForeColor = Color.Green;
                lblgrid.Text = "تم التحديث بنجاح";
                gv_Exam_Sub.EditIndex = -1;
                gv_Exam_SubBind();
            }
            else
            {

                
                lblgrid.ForeColor = Color.Red;
                lblgrid.Text = "لم تتم عملية التحديث..حاول مرة اخري";
            }
            lblgrid.Visible = true;
        }
    }
}


